#!/bin/bash

#####################################################
##
## Given a solution file and a file with solutions
## check if the solution in the first file matches
## one of the solutions of the second.
##
#####################################################

SOLUTION_TO_TEST=$1
SOLUTIONS=$2

LARGEST_VAR=0

for lit in `cat $SOLUTION_TO_TEST`
do

	if [[ "$lit" != "0" ]]
        then
                VAR=`echo $lit | sed s/"-"//g`
                AUX=`echo $lit | grep -c "-"`

                 if [[ $AUX == "1" ]]
                 then
                         SIGN=f
                 else
                         SIGN=t
                 fi

                let INDEX=${VAR}-1
                RESULTS_VEC[$INDEX]=$SIGN

		if [ $VAR -gt $LARGEST_VAR ]
		then
			LARGEST_VAR=$VAR
		fi

        fi

done

let LAST_VAR=$LARGEST_VAR-1

for i in $(seq 0 $LAST_VAR)
do

	if [[ ${RESULTS_VEC[$i]} != "f" ]] && [[ ${RESULTS_VEC[$i]} != "t" ]]
	then
		RESULTS_VEC[$i]="u"
	fi
done

MATCHED="f"

while read line
do
	CORRECT_VARIABLES=0
	TOTAL_VARS=0

        for lit in $line
        do

                if [[ "$lit" != "0" ]]
                then
                        VAR=`echo $lit | sed s/"-"//g`
                        AUX=`echo $lit | grep -c "-"`

                        if [[ $AUX == "1" ]]
                        then
                                SIGN=f
                        else
                                SIGN=t
                        fi

                        let INDEX=${VAR}-1
                        RES=${RESULTS_VEC[$INDEX]}

                        if [[ "$RES" == "$SIGN" ]] || [[ "$RES" == "u" ]] || [[ "$INDEX" -gt "$LAST_VAR" ]]
                        then
                                let CORRECT_VARIABLES++
                        fi

			let TOTAL_VARS++

                fi

        done

	if [[ "$TOTAL_VARS" == "$CORRECT_VARIABLES" ]]
	then
		MATCHED="t"
	fi


done < $SOLUTIONS

if [[ "$MATCHED" == "t" ]]
then
	echo "Results match!"
	exit 0
else

	echo "Results do not match!"
	exit 1
fi
